//
//  NSDate+RSTool.m
//  TJYDZFTerminal
//
//  Created by thinkjoy on 2017/3/20.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "NSDate+RSTool.h"

@implementation NSDate (RSTool)

#pragma mark    计算StartDay和endDay差数
- (NSInteger)computeNumDayToDate:(NSDate *)date
//                  getStartDayStr:(NSString *)startDay lessThanEndDayStr:(NSString *)endDay
{
    //当前时间晚于date，直接返回0
    if ([[self laterDate:date] isEqualToDate:self]) {
        if ([self isEqualToDate:date]) {
            return 1;
        }else{
            NSDate  * startDate =   date;
            NSDate  * endDate =   self;
            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
            unsigned int unitFlag = NSCalendarUnitDay;
            NSDateComponents *components = [calendar components:unitFlag fromDate:startDate toDate:endDate options:0];
            return [components day] + 1;
        }
    }else{
        NSDate  * startDate =   self;
        NSDate  * endDate =   date;
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        unsigned int unitFlag = NSCalendarUnitDay;
        NSDateComponents *components = [calendar components:unitFlag fromDate:startDate toDate:endDate options:0];
        
        return [components day] + 1;
    }
}
#pragma mark    day1>=day2
- (BOOL)isGreaterThanOrErqalDayStr:(NSDate *)date
{
    if ([self isEqualToDate:date]) {
        return YES;
    }
    if ([[self laterDate:date] isEqualToDate:self]) {
        return YES;
    }
    return NO;
}

#pragma mark    使用forMartStr格式化Date
/**
 使用forMartStr格式化Date，forMartStr默认为“yyyy-MM-dd HH:mm”
 
 @param forMartStr forMartStr默认为“yyyy-MM-dd HH:mm”
 @return 格式化后的时间字符串
 */
- (NSString *)toStrWithFormartStr:(NSString *)forMartStr
{
    if (forMartStr.length<1) {
        forMartStr  =   @"yyyy-MM-dd HH:mm";
    }
    NSDateFormatter * dateFormatter =   [[NSDateFormatter alloc] init];
    //强制设置为英文环境，适配中文（真机）环境下也能正常转换
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
    
    [dateFormatter setDateFormat:forMartStr];
    return [dateFormatter stringFromDate:self];
}

- (NSInteger)numberOfDaysInCurrentMonth {
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self].length;
}

- (NSInteger)weeklyOfCurrentDay {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.firstWeekday = 2;
    return [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfYear forDate:self];
}

- (NSDate *)firstDayOfCurrentMonth {
    NSDate *firstDay = nil;
    BOOL isOk = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&firstDay interval:NULL forDate:self];
    if (!isOk) {
        NSLog(@"获取失败");
    }
    return firstDay;
}

- (NSInteger)yearOfCurrentDate {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self];
    return components.year;
}

- (NSInteger)monthOfCurrentDate {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self];
    return components.month;
}

- (NSInteger)dayOfCurrentDate {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self];
    return components.day;
}
#pragma mark    获取date所在的时
- (NSInteger)hourOfCurrentDate
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:self];
    return components.hour;
}
#pragma mark    获取date所在的分钟
- (NSInteger)minuOfCurrentDate
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:self];
    return components.minute;
}

#pragma mark    一周以周日为第一天，计算当前月在日历上的行数
- (NSInteger)getRowsByWeekFirstDayIsSun
{
    NSInteger days = [self numberOfDaysInCurrentMonth];
    NSInteger firstWeekly = [[self firstDayOfCurrentMonth] weeklyOfCurrentDay];
    NSInteger line = (days + firstWeekly - 1) / 7;
    return line+1;
}


@end
